﻿@using MudBlazor.Utilities 
@namespace MudBlazor

<div class="mud-treeview-item-arrow">
    @if (Visible)
    {
        <MudIconButton OnClick="@ToggleAsync" Icon="@(Loading ? LoadingIcon : ExpandedIcon)" Color="@(Loading ? LoadingIconColor : ExpandedIconColor)" Class="@Classname"></MudIconButton>
    }
</div>
 
@code {
    protected string Classname =>
    new CssBuilder()
        .AddClass("mud-treeview-item-arrow-expand mud-transform", Expanded && !Loading)
        .AddClass("mud-treeview-item-arrow-load", Loading)
    .Build();

    /// <summary>
    /// If true, displays the button.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Visible { get; set; }

    /// <summary>
    /// Determens when to flip the expanded icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Expanded { get; set; }

    /// <summary>
    /// If true, displays the loading icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Loading { get; set; }

    /// <summary>
    /// Called whenever expanded changed.
    /// </summary>
    [Parameter] public EventCallback<bool> ExpandedChanged { get; set; }

    /// <summary>
    /// The loading icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public string LoadingIcon { get; set; } = Icons.Material.Filled.Loop;

    /// <summary>
    /// The color of the loading. It supports the theme colors.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public Color LoadingIconColor { get; set; } = Color.Default;

    /// <summary>
    /// The expand/collapse icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public string ExpandedIcon { get; set; } = Icons.Material.Filled.ChevronRight;

    /// <summary>
    /// The color of the expand/collapse. It supports the theme colors.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public Color ExpandedIconColor { get; set; } = Color.Default;


    private Task ToggleAsync()
    {
        Expanded = !Expanded;
        return ExpandedChanged.InvokeAsync(Expanded);
    }
}
